﻿// -----------------------------------------------------------------------------
// <copyright file="Settings.cs" company="Justin Wallace Saunders">
//     Copyright (c) Microsoft Public License. All rights reserved.
// </copyright>
// <author name="Justin Wallace Saunders" email="justinsa@live.ca" />
// -----------------------------------------------------------------------------
namespace Community.Tools.Assembly.Configuration.Loader.Example
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Settings class.
    /// </summary>
    public static class Settings
    {
        /// <summary>
        /// The global dictionary key for configuration settings.
        /// </summary>
        private const string AssemblyDictionaryKey = @"Community.Tools.Assembly.Configuration.Loader.Example.dll.config";

        /// <summary>
        /// Gets a configuration value.
        /// </summary>
        public static int ConfigurationValue
        {
            get
            {
                return int.Parse(GetSetting(@"ConfigurationValue"), CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the class has been initialized.
        /// </summary>
        private static bool Initialized { get; set; }

        /// <summary>
        /// Gets the value of a key from the App Setting section of the
        /// assembly configuration file.
        /// </summary>
        /// <param name="key">The key to look-up</param>
        /// <returns>The value related to the key</returns>
        public static string GetSetting(string key)
        {
            if (!Initialized)
            {
                lock (typeof(Settings))
                {
                    if (!Initialized)
                    {
                        Initialize();
                    }
                }
            }

            return Community.Tools.Assembly.Configuration.Loader.Settings.GetSetting(AssemblyDictionaryKey, key);
        }

        /// <summary>
        /// Initializes the settings.
        /// </summary>
        private static void Initialize()
        {
            Initialized = Community.Tools.Assembly.Configuration.Loader.Settings.LoadDictionary(AssemblyDictionaryKey, typeof(Settings));
            if (!Initialized)
            {
                // This will only happen if the assembly configuration file was
                // not loaded by the calling application nor found in the bin folder.
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} file not loaded", AssemblyDictionaryKey));
            }
        }
    }
}
